package com.xia.service.impl;

import com.xia.dao.users.IUsersDao;
import com.xia.domian.Users;
import com.xia.service.IUserService;
import com.xia.util.exception.CodeEnum;
import com.xia.util.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 用户业务实现层
 */

@Service
public class IUserServiceImpl implements IUserService {
    private IUsersDao usersDao;

    @Resource
    private String getDate;

    @Value("${online.leading-end-agreement}")
    private String leadingEndAgreement;

    @Autowired
    public void setUsersDao(IUsersDao usersDao) {
        this.usersDao = usersDao;
    }

    @Override
    public Users login(Users users){
        Users usersData = usersDao.selectUsersNameAndPassword(users.getUsersName(), users.getUsersPassword());
        if (usersData == null) {
            throw new ServiceException(CodeEnum.CODE_400,"请检查您的用户名和密码");
        }
        Users usersUpdate= new Users();
        usersUpdate.setUsersId(usersData.getUsersId());
        usersUpdate.setUsersIp(users.getUsersIp());
        usersUpdate.setUsersLocation(users.getUsersLocation());
        int updateUsersIP=usersDao.updateUsers(usersUpdate);
        if (updateUsersIP == 0){
            throw new ServiceException(CodeEnum.CODE_400,"系统错误请稍后");
        }
        return usersData;
    }

    @Override
    public void registerUsers(Users users){
        users.setUsersCreationTime(getDate);
        users.setUsersFirstImage(leadingEndAgreement+"/static/img/用户背景图2.jpg");
        if (users.getUsersPortrait() == null){
            users.setUsersPortrait(leadingEndAgreement+"/static/img/头像2.jpg");
        }
        if (usersDao.insertUsers(users) == 0){
            throw new ServiceException(CodeEnum.CODE_400,"注册失败！");
        }
    }
}
